package com.example.erp.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.erp.entity.Admin;
import com.example.erp.entity.Goods;
import com.example.erp.entity.InoutBound;
import com.example.erp.entity.InoutBoundDtl;
import com.example.erp.entity.dto.ExcelDto;
import com.example.erp.entity.dto.InoutBoundDto;
import com.example.erp.mapper.AdminMapper;
import com.example.erp.mapper.GoodsMapper;
import com.example.erp.mapper.InoutBoundDtlMapper;
import com.example.erp.mapper.InoutBoundMapper;
import com.example.erp.service.IAdminService;
import com.example.erp.service.IGoodsService;
import com.example.erp.service.IInoutBoundDtlService;
import com.example.erp.service.IInoutBoundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 出入库母单 服务实现类
 * </p>
 *
 * @author rws
 * @since 2025-01-08
 */
@Service
public class InoutBoundServiceImpl extends ServiceImpl<InoutBoundMapper, InoutBound> implements IInoutBoundService {



    @Autowired
    private IInoutBoundDtlService inoutBoundDtlService;
    @Autowired
    private InoutBoundMapper inoutBoundMapper;
    @Autowired
    private InoutBoundDtlMapper inoutBoundDtlMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private AdminMapper adminMapper;


    LocalDateTime now = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String time = now.format(formatter);
    @Override
    public void insertInoutBound(List<ExcelDto> excelDtoList) {


        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMdd");
        String num = now.format(formatter2);
        //入库
        InoutBound inBound=new InoutBound();
        inBound.setType(1)
                .setStatus(2)
                .setBoundNum(num)
                .setCreateBy(StpUtil.getLoginIdAsInt())
                .setUpdateBy(StpUtil.getLoginIdAsInt())
                .setCreateTime(time)
                .setUpdateTime(time);

        inoutBoundMapper.insert(inBound);
        int id=inBound.getBoundId();
        inoutBoundDtlService.insertIn_bound_dtl(excelDtoList,id);

        //预出库
        InoutBound outBound=new InoutBound();
        inBound.setType(2)
                .setStatus(1)
                .setBoundNum(num)
                .setCreateBy(StpUtil.getLoginIdAsInt())
                .setUpdateBy(StpUtil.getLoginIdAsInt())
                .setCreateTime(time)
                .setUpdateTime(time);
        inoutBoundMapper.insert(outBound);
        id=outBound.getBoundId();
        inoutBoundDtlService.insertout_bound_dtl(excelDtoList,id);








    }

    //查询入库表
    @Override
    public List<InoutBoundDto> selectInBound() {
        QueryWrapper<InoutBoundDtl> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 1);
        List<InoutBoundDtl>inoutBoundDtlList=inoutBoundDtlMapper.selectList(queryWrapper);


        List<InoutBoundDto>inoutBoundDtoList=new ArrayList<>();
        for(InoutBoundDtl inoutBoundDtl:inoutBoundDtlList){
            //查询母单id
            QueryWrapper<InoutBound> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("bound_id", inoutBoundDtl.getBoundId());
            InoutBound inoutBound=inoutBoundMapper.selectOne(queryWrapper2);

            //查询商品名称
            QueryWrapper<Goods> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("goods_code", inoutBoundDtl.getGoodsCode());
            Goods goods=goodsMapper.selectOne(queryWrapper3);

            //查询修改人
            QueryWrapper<Admin> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.eq("admin_id", inoutBound.getUpdateBy());
            Admin admin=adminMapper.selectOne(queryWrapper4);


            InoutBoundDto inoutBoundDto=new InoutBoundDto();
            inoutBoundDto.setBoundNum(inoutBound.getBoundNum())
                    .setBoundId(inoutBound.getBoundId())
                    .setInfoId(inoutBoundDtl.getInfoId())
                    .setGoodsCode(inoutBoundDtl.getGoodsCode())
                    .setGoodsName(goods.getGoodsName())
                    .setType(inoutBoundDtl.getType())
                    .setNum(inoutBoundDtl.getNum())
                    .setWeight(inoutBoundDtl.getWeight())
                    .setUpdateBy(admin.getName())
                    .setCreateTime(time)
                    .setUpdateTime(time);
            inoutBoundDtoList.add(inoutBoundDto);
        }
        return inoutBoundDtoList;
    }

    @Override
    public List<InoutBoundDto> selectOutBound() {
        QueryWrapper<InoutBoundDtl> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 2);
        List<InoutBoundDtl>inoutBoundDtlList=inoutBoundDtlMapper.selectList(queryWrapper);


        List<InoutBoundDto>inoutBoundDtoList=new ArrayList<>();
        for(InoutBoundDtl inoutBoundDtl:inoutBoundDtlList){
            //查询母单id
            QueryWrapper<InoutBound> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("bound_id", inoutBoundDtl.getBoundId());
            InoutBound inoutBound=inoutBoundMapper.selectOne(queryWrapper2);

            //查询商品名称
            QueryWrapper<Goods> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("goods_code", inoutBoundDtl.getGoodsCode());
            Goods goods=goodsMapper.selectOne(queryWrapper3);

            //查询修改人
            QueryWrapper<Admin> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.eq("admin_id", inoutBound.getUpdateBy());
            Admin admin=adminMapper.selectOne(queryWrapper4);


            InoutBoundDto inoutBoundDto=new InoutBoundDto();
            inoutBoundDto.setBoundNum(inoutBound.getBoundNum())
                    .setBoundId(inoutBound.getBoundId())
                    .setInfoId(inoutBoundDtl.getInfoId())
                    .setGoodsCode(inoutBoundDtl.getGoodsCode())
                    .setGoodsName(goods.getGoodsName())
                    .setType(inoutBoundDtl.getType())
                    .setNum(inoutBoundDtl.getNum())
                    .setWeight(inoutBoundDtl.getWeight())
                    .setUpdateBy(admin.getName())
                    .setCreateTime(time)
                    .setUpdateTime(time);
            inoutBoundDtoList.add(inoutBoundDto);
        }
        return inoutBoundDtoList;
    }

    //更新数量,重量
    @Override
    public void updateBound(InoutBoundDto inoutBoundDto) {
        InoutBound inoutBound =new InoutBound();
        inoutBound.setBoundId(inoutBoundDto.getBoundId())
                .setUpdateBy(StpUtil.getLoginIdAsInt())
                .setUpdateTime(time);
        inoutBoundMapper.updateById(inoutBound);

        InoutBoundDtl inoutBoundDtl=new InoutBoundDtl();
        inoutBoundDtl.setDtlId(inoutBoundDto.getDtlId())
                .setNum(inoutBoundDto.getNum())
                .setWeight(inoutBoundDto.getWeight())
                .setUpdateTime(time);
        inoutBoundDtlMapper.updateById(inoutBoundDtl);
    }



}
